package com.avicit.zipkinkafkaesserver.controller;

import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceDuration;
import com.avicit.zipkinkafkaesserver.service.ZipkinInterfaceDurationService;
import com.avicit.zipkinkafkaesserver.vo.InterfaceCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Controller
public class ZipkinInterfaceDurationController {

    @Autowired
    private ZipkinInterfaceDurationService service;

    @GetMapping("/api/interfaceDurationCountList")
    public String query(long startTime, long endTime,
                      Model model,
                      @RequestParam(required = false, defaultValue = "0") long min,
                      @RequestParam(required = false, defaultValue = "0") long max,
                      @RequestParam(required = false, defaultValue = "0") int topN) {
        List<ZipkinInterfaceDuration> query = service.query(startTime, endTime, min, max, topN);
        List<InterfaceCountVo> collect = query.stream().map(item -> {
            double interfaceDuration = item.getInterfaceDuration();
            InterfaceCountVo build = InterfaceCountVo
                            .builder()
                            .ip(item.getInterfaceIp())
                            .serverName(item.getServerName())
                            .interfaceName(item.getInterfacePath())
                            .duration((int) interfaceDuration)
                            .build();
                    return build;
                }

        ).collect(Collectors.toList());
        model.addAttribute("countVos",collect);
        return "apiInterfaceDurationCountList";
    }
}
